using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MKDS_Course_Editor.GCN.Sound
{
    public partial class BAA : Form
    {
        string file;
        public BAA(string file)
        {
            InitializeComponent();
            this.file = file;
        }
        System.Media.SoundPlayer s = new System.Media.SoundPlayer();
        private void BAA_Load(object sender, EventArgs e)
        {
            List<BAAD.BaaContentFile> ar = BAAD.ReadBAA(file);
            if (!Directory.Exists(@"C:\BAA\")) Directory.CreateDirectory(@"C:\BAA\");
            string dir = Path.GetDirectoryName(file) + @"\Waves";
            if (!Directory.Exists(dir))
            {
            start:
                if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                    && folderBrowserDialog1.SelectedPath.Length > 0)
                {
                    dir = folderBrowserDialog1.SelectedPath;
                    if (Directory.GetFiles(dir, "*.aw").Length == 0)
                    {
                        goto start;
                    }
                }
                else
                {
                    return;
                }
            }
            List<WSYS.AWFileContent[]> aw = new List<WSYS.AWFileContent[]>();
            for (int i = 0; i < ar.Count; i++)
            {
                treeView1.Nodes.Add(i.ToString()+"."+ar[i].Filetype.ToString());
                if (ar[i].Filetype == BAAD.FileType.ws)
                {
                    aw.Add(WSYS.ReadWS(ar[i].Content, dir).ToArray());
                    for(int j = 0; j < aw.Last().Length; j++)
                    {
                        treeView1.Nodes[i].Nodes.Add(aw.Last()[j].name);
                        for (int k = 0; k < aw.Last()[j].SubFiles.Count; k++)
                        {
                            TreeNode n = new TreeNode(k.ToString());
                            n.Tag = aw.Last()[j].SubFiles[k];
                            treeView1.Nodes[i].Nodes[j].Nodes.Add(n);
                        }
                    }
                }
                /*string name = @"C:\BAA\" + i.ToString() + "." + ar[i].Filetype.ToString();
                File.Create(name).Close();
                File.WriteAllBytes(name, ar[i].Content);*/
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag is byte[])
            {
                s.Stream = new MemoryStream(e.Node.Tag as byte[]);
                s.Load();
                toolStripButton1.Enabled = true;
                File.Create(@"C:\test.wav").Close();
                File.WriteAllBytes(@"C:\test.wav", e.Node.Tag as byte[]);
            }
            else
            {
                toolStripButton1.Enabled = false;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            s.PlaySync();
        }
    }
}
